<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理系统</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/my_css.css">
</head>

<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="my-logo">订单管理系统</div>
        </div>
    </div>

    <div class="my-signup-form">
        <form class="layui-form" style="text-align: center">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="username" name="username" placeholder="请输入用户名"
                               autocomplete="off" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="password1" name="password1" placeholder="请输入密码"
                               autocomplete="off" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">确认密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="password2" name="password2" placeholder="请输入密码"
                               autocomplete="off" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline" style="width: 300px">
                    <label class="layui-form-label">用户类型</label>
                    <div class="layui-input-block">
                        <select id= "user_type" name="user_type" lay-filter="user_type" lay-verify="required">
                            <option value=""></option>
                            <option value="0">普通用户</option>
                            <option value="1">商家用户</option>
                            <option value="2">物流人员</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="signup">注册</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
        layui.use('form', function() {
            let form = layui.form;

            form.render();
            form.on('submit(signup)', function(data) {
                let username = data.field.username;
                let password1 = data.field.password1;
                let password2 = data.field.password2;
                let type = $('#user_type option:selected').val();

                if (password1 != password2) {
                    layer.msg('两次输入的密码不相等!', {
                        icon: 2,
                        time: 2000
                    });
                    return false;
                }

                let user = {};
                user['username'] = username;
                user['password'] = password1;
                user['type'] = parseInt(type);
                $.ajax({
                    url: 'sign_up',
                    dataType: 'json',
                    data: user,
                    method: 'post',
                    success: function (res) {
                        $("#username").val('');
                        $("#password1").val('');
                        $("#password2").val('');
                        if (res) {
                            window.location.href = 'index.html';
                        } else {
                            layer.msg('用户名已存在!', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function () {
                        layer.msg('注册错误!', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                return false;
            });
        });
    </script>

</body>



</html>